<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/5/15
 * Time: 17:39
 */

namespace api\controllers;


use common\controllers\ApiCommonCtrl;
use common\services\WorkSve;
use common\services\WorksClassSve;
use common\services\MemberSve;
use Yii;

class WorkController extends ApiCommonCtrl
{
    /**
     * 获取列表
     * @return array
     */
    public function actionList()
    {
        $this->checkLogin();
        //实例化会员服务类
        $WorkSve = new WorkSve();
        return $WorkSve->getList(['member_id' => $this->TokenObj->member_id], 'start_time DESC,work_id ASC');
    }

    /**
     * 添加
     * @return array
     */
    public function actionAdd()
    {
        $request = Yii::$app->request;

        if ($request->isPost) {
            $this->checkLogin();
            //实例化会员服务类
            $data = $request->post();
            $data['member_id'] = $this->TokenObj->member_id;
            $MemberSve = new MemberSve();
            $memberInfo = $MemberSve->getInfo(['member_id' => $this->TokenObj->member_id]);
            $data['member_name'] = $memberInfo['data']['Obj']['nickname'];
            $WorkSve = new WorkSve();
            return $WorkSve->add($data);
        }
    }

    /**
     * 查看
     * @return array
     */
    public function actionGetInfo()
    {
        $today = strtotime(date('Y-m-d'));
        $request = Yii::$app->request;
        $this->checkLogin();

        //实例化会员服务类
        $workId = $request->get('work_id');
        $WorkSve = new WorkSve();
        $re = $WorkSve->getInfo(['and', ['=', 'work_id', $workId], ['=', 'member_id', $this->TokenObj->member_id]]);
        if ($re['code'] === 0) {
            $re['data']['Obj'] = $re['data']['Obj']->toArray();
            if ($re['data']['Obj']['end_time'] > time()) {
                $re['data']['Obj']['is_edit'] = 1;
            } else {
                $re['data']['Obj']['is_edit'] = 0;
            }
        }
        return $re;
    }


    /**
     * 删除
     * @return string
     */
    public function actionRemove()
    {
        $today = strtotime(date('Y-m-d'));
        $request = Yii::$app->request;
        $this->checkLogin();

        //实例化会员服务类
        $workId = $request->get('work_id');

        $WorkSve = new WorkSve();
        $re = $WorkSve->getInfo(['work_id' => $workId]);
        if ($re['code'] === 0) {
            if ($re['data']['Obj']['end_time'] > time()) {
                return $WorkSve->remove(['and', ['=', 'work_id', $workId], ['=', 'member_id', $this->TokenObj->member_id], ['>', 'end_time', $today]]);
            } else {
                return $this->asJson(['code' => -1, 'msg' => '不能删除']);
            }
        }

    }


    /**
     * 自己查看
     * @return array
     */
    public function actionInfoByMySelf()
    {
        $this->checkLogin();
        $WorkSve = new WorkSve();
        return $WorkSve->getList(['member_id' => $this->TokenObj->member_id], 'start_time DESC', true);
    }

    /**
     * 查看别人
     * @return array
     */
    public function actionInfoByAll()
    {
        $request = Yii::$app->request;
        $this->checkLogin();
        $WorkSve = new WorkSve();
        return $WorkSve->getList(['member_id' => $request->get('member_id')], 'start_time DESC,work_id ASC', true);
    }

    /**
     * 统计
     * @return string
     */
    public function actionCountByMySelf()
    {
        $this->checkLogin();
        $WorkSve = new WorkSve();
        $where = ['and'];
        $request = Yii::$app->request;
        $startTime = $request->post('start_time', 0);
        $endTime = $request->post('end_time', time());
        array_push($where, ['>', 'start_time', $startTime]);
        array_push($where, ['<', 'start_time', $endTime]);
        array_push($where, ['=', 'member_id', $this->TokenObj->member_id]);

        $WorksClassSve = new WorksClassSve();
        $worksClassInfo = $WorksClassSve->getList(['is_show' => 1], 'sort DESC');
        $reData = [];
        foreach ($worksClassInfo['data']['list'] as $key => $data) {
            $tem = [];
            array_push($where, ['=', 'work_class_id', $data['works_class_id']] );
            $re = $WorkSve->getList($where, 'start_time DESC', true);
            $tem['works_class'] = $data['class_name'];
            $tem['work_count'] = $re['data']['pageArr']['total'];
            array_push($reData, $tem);
        }
        return $this->asJson(['code'=>0,'msg'=>'请求成功','data'=>$reData]);
    }


    /**
     * 展示
     * @return array
     */
    public function actionShowInfo()
    {
        $WorkSve = new WorkSve();

        $now = time();
        $today = strtotime(date('Y-m-d'));
        $temArr = [];
        //$re = $WorkSve->getList(['and',['>','start_time1',$now],['<','end_time',$now],['>','start_time',$today]],'start_time DESC,end_time ASC,work_id DESC');
        $re = $WorkSve->getShowTvList([], 'start_time desc');
        $MemberSve = new MemberSve();
        foreach ($re['data']['list'] as $key => $data) {
            $tem = $data->toArray();
            unset($tem['remark']);
            $memberInfo = $MemberSve->getInfo(['member_id' => $data['member_id']]);
            $avatar = '';

            if ($memberInfo['code'] == 0) {
                $avatar = $memberInfo['data']['Obj']->avatar;
            }
            $tem['avatar'] = $avatar ? HTTP_DOMAIN_ASSETS . '/' . $avatar : '';
            $tem['office'] = $memberInfo['data']['Obj']->office;
            array_push($temArr, $tem);
        }
        $re['data']['list'] = $temArr;
        return $re;
    }
}